package com.sectop.bank;

public class CheckingAccount extends Account{
//    private double interestRate;
//    public CheckingAccount(double balance,double interestRate) {
//        super(balance);
//        this.interestRate = interestRate;
//    }
//
//    @Override
//    public boolean withDraw(double money) {
//        return super.withDraw(money);
//    }
//private  double overdraftProtection;
private SavingAccount protectedBy;

    public CheckingAccount(double balance) {
        super(balance);
        this.protectedBy = null;
    }

    public CheckingAccount(double balance, SavingAccount protectedBy) {
        super(balance);
        this.protectedBy = protectedBy;
    }




    @Override
    public boolean withDraw(double atm) {

        if(protectedBy == null){
            if((this.getBalance() - atm) < 0){
                return false;
            }else {
                balance -= atm;
                return true;
            }
        }
         if((protectedBy.getBalance() + this.getBalance()) - atm < 0){
             return false;
         }else if(this.balance - atm > 0){
               this.balance -= atm;


         } else if (this.balance - atm < 0) {

             double down = this.balance - atm;
             this.balance = 0;
             protectedBy.balance += down;
         }
         return true;
    }
}
